package com.zjpavt.taiyuan.schedule;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.zjpavt.taiyuan.utils.HttpClientUtil;
import lombok.extern.log4j.Log4j;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
@EnableScheduling
@Log4j
public class H3cAuthenticationScheduleTask {
    private static final String url = "http://10.20.30.201:8080/cas/spring_check?encrypt=true&lang=cn&name=WpIxDAPVsRM%3D&password=WpIxDAPVsRM%3D";
    private Gson gson = new GsonBuilder().create();

    @Autowired
    private RedisTemplate redisTemplate;
    @Scheduled(fixedRate = 30*60*1000L)
//    @PostConstruct
    public void authentication() {
        try {
            PostMethod postMethod = new PostMethod(url);
            String post = HttpClientUtil.post(postMethod, null, null);
            JsonObject jsonObject = gson.fromJson(post, JsonObject.class);
            String sessionId = gson.fromJson(jsonObject.get("sessionId"), String.class);
            redisTemplate.boundValueOps("sessionId").set(sessionId);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }
}
